package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.RoleMapper;
import cn.tedu.fruitshop.mapper.URMapper;
import cn.tedu.fruitshop.mapper.UserMapper;
import cn.tedu.fruitshop.pojo.vo.RoleListItemVO;
import cn.tedu.fruitshop.pojo.vo.UserListItemVO;
import cn.tedu.fruitshop.pojo.vo.UserStandardVO;
import cn.tedu.fruitshop.service.RoleServer;
import cn.tedu.fruitshop.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RoleServiceimpl implements RoleServer {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    URMapper urMapper;

    @Autowired
    UserMapper userMapper;
    /*
    * 查询所有用户列表
    * */
    @Override
    public List<UserListItemVO> userList() {
        log.debug("开始处理userList");
        List<UserListItemVO> userListItemVOS = roleMapper.userList();
        return userListItemVOS;
    }

    @Override
    public List<UserListItemVO> businessList() {
        log.debug("开始处理businessList");
        List<UserListItemVO> userListItemVOS = roleMapper.businessList();
        return userListItemVOS;
    }

    @Override
    public List<UserListItemVO> adminList() {
        log.debug("开始处理adminList");
        List<UserListItemVO> userListItemVOS = roleMapper.adminList();
        return userListItemVOS;
    }

    @Override
    public void delete(Integer id) {
        UserStandardVO standardById = userMapper.getStandardById(id);
        log.debug("standardById:"+standardById);
        if(standardById == null){
            String message = "删除用户失败，尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i = roleMapper.deleteById(id);
        if (i<1){
            String message = "删除用户失败,服务器错误";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

        int i1 = urMapper.deleteById(id);
        if(i1<1){
            String message = "删除失败,服务器错误";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
